#include <iostream>
#include <unordered_map>
using namespace std;

// 题解： https://murfyexp.github.io/2017/02/27/%E7%AE%97%E6%B3%95/Leetcode/8-5/

class Node {
  public:
    int val;
    Node* left, *right;
    Node(int val) {
        this->val = val;
        this->left = nullptr;
        this->right = nullptr;
    }
};
Node* creattree(Node* root, int n) {
    if (n == 0) {
        return nullptr;
    }
    int fa, lch, rch, val;
    cin >> fa >> lch >> rch >> val;
    root = new Node(val);
    if (lch != 0) {
        Node* node = new Node(lch);
        root->left = creattree(node, --n);
    }
    if (rch != 0) {
        Node* node = new Node(rch);
        root->right = creattree(node, --n);
    }
    return root;
}
int preorder(Node* head,int sum,int presum,int level,int maxlen, unordered_map<int, int> &summap){
    if(head == nullptr){
        return maxlen;
    }
    int cursum = presum + head->val;
    if(!summap.count(cursum)){
        summap[cursum] = level;
    }
    if(summap.count(cursum - sum)){
        maxlen = max(maxlen, level - summap[cursum - sum]);
    }
    maxlen = preorder(head->left, sum, cursum, level + 1, maxlen, summap);
    maxlen = preorder(head->right, sum, cursum, level + 1, maxlen, summap);
    if(level == summap[cursum]){
        summap.erase(cursum);
    }
    return maxlen;
}
int getmaxlen(Node* head,int sum){
    unordered_map<int, int> summap;
    summap[0] = 0;
    return preorder(head, sum, 0, 1, 0, summap);
}
int main() {
    int n, root, sum;
    cin >> n >> root;
    Node* head = nullptr;
    head = creattree(head, n);
    cin >> sum;
    cout << getmaxlen(head, sum) <<endl;
}